﻿using MediatR;
using RBAC.Common.Domains.RabbitProject;
using RBAC.Common.Dto;
using RBAC.Common.infrastructures.interfaces;
using RBAC.Enum;

namespace RBAC.api.Application.commond.FarmingInformation
{
    public class FarmingEnterpriseInformationCreateHander : IRequestHandler<FarmingEnterpriseInformationCreateCommond, Rulst>
    {

        private readonly IbaseRpository<breedMess> _breedMess;

        public FarmingEnterpriseInformationCreateHander(IbaseRpository<breedMess> breedMess)
        {
            _breedMess = breedMess;
        }

        public async Task<Rulst> Handle(FarmingEnterpriseInformationCreateCommond request, CancellationToken cancellationToken)
        {
            var query = new Rulst() { Code = (int)Reult.success };

            ///这里的添加我们的养殖企业信息
            ///和我们的添加同理 都是需要去判断因为你不去做判断一直去添加 数据会出现 数据不对等 脏数据
           var result= _breedMess.Query().Where(x=>x.ProDuctionBasicid==request.ProDuctionBasicid).FirstOrDefault();
            if (result != null)
            {
                  result.Farm=request.Farm;
                  result.FarmName=request.FarmName;
                   result.Framnum=request.Framnum;
                _breedMess.update(result);
            }
            else
            {
                var gh = new breedMess()
                {
                    varietyName = "",
                    Breedcome = "",
                    BreedDay = 0,
                    Fodderbrand = "",
                    FodderName = "",
                    ColumnTime = new DateTime(),
                    Farm = result.Farm,
                    FarmName = result.FarmName,
                    Framnum = result.Framnum,
                    ProDuctionBasicid = request.ProDuctionBasicid,
                    Vaccineid = 0
                };

                _breedMess.Add(gh);


            }

            return query;
        }
    }
}
